package com.matrix.media.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.matrix.media.utils.FileUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class VideoTrack {
    //默认为普通视频轨。
    //
    //当Type=Effect时，当前轨道可当做特效轨使用，VideoTrackClips可填入EffectTrackItem。
    @JSONField(name = "Type")
    private String Type;

    @JSONField(name = "MainTrack")
    //用于指定当前轨道是否为主轨道。默认为 False。详细介绍及示例请参见设置主轨道。
    private String MainTrack;

    //视频轨素材片段列表。 必填
    @JSONField(name = "VideoTrackClips")
    private List<VideoTrackClip> VideoTrackClips;


    /**
     * 设置视频url列表
     * @param urls
     */
    public void handleVideoURLs(List<String> urls) {

        List<VideoTrackClip> tmpTrackClips = new ArrayList<>();
        for (String url: urls) {
            if (FileUtil.isVideo(url)) {
                VideoTrackClip trackClip = new VideoTrackClip();
                trackClip.setMediaURL(url);
                trackClip.setType("Video");
                tmpTrackClips.add(trackClip);
            } else if (FileUtil.isImage(url)) {
                VideoTrackClip trackClip = new VideoTrackClip();
                trackClip.setMediaURL(url);
                trackClip.setType("Image");
                tmpTrackClips.add(trackClip);
            }
        }
        this.setVideoTrackClips(tmpTrackClips);
    }

    private String getFileExtension(String url) {
        int lastDotIndex = url.lastIndexOf('.');
        if (lastDotIndex > 0 && lastDotIndex < url.length() - 1) {
            return url.substring(lastDotIndex + 1);
        } else {
            return "";
        }
    }
}
